package View;
/*
 * Written by Phat Le
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import FileParser.FileParser;
import Model.Conference;
import Model.Headquarters;
import Model.Roles;
import Model.User;

/**
 * Creates view for headquarters to create conference and assign pc.
 * @author Phat
 *
 */
public class HeadquartersView extends JFrame{
	private static final long serialVersionUID = 1L;  //to get rid of warning
	private static final int WIDTH = 850;
	private static final int HEIGHT = 600;
	
	private static final int BUTTON_WIDTH = 100;
	private static final int BUTTON_HEIGHT = 30;
	private static final int TOP_PANEL_HEIGHT = 3;
	private static final int MID_PANEL_HEIGHT = 10;
	private static final int BOT_PANEL_HEIGHT = 3;
	private static final int FRAME_HEIGHT = 16;
	private final JFrame mainFrame;
	private Headquarters hq;
	private JTextField confText;
	private JTextField dateText;
	private JTextField venueText;
	private JTextField deadlineText;
	private JComboBox pcBox;
	private FileParser fp;

	public HeadquartersView(final FileParser a_fp, final Headquarters a_hq){
		mainFrame = new JFrame("Headquarters User");
		fp = a_fp;
		hq = a_hq;
	}
		
	public void start() {
	
		mainFrame.setSize(WIDTH, HEIGHT);
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);		//set in the middle of the screen
		mainFrame.setLayout(new BorderLayout());
		mainFrame.add(createTopPanel(), BorderLayout.NORTH);  //use the enum Roles for the role string
		mainFrame.add(createMidPanel(), BorderLayout.WEST);
		mainFrame.add(createBotPanel(), BorderLayout.SOUTH);
		mainFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		mainFrame.setVisible(true);
	}
	
	/**
	 * Creates the top panel that contains name, conference, & role info.
	 */
	private JPanel createTopPanel() {
		
		JPanel topPnl = new JPanel();
		topPnl.setBackground(Color.LIGHT_GRAY);	
		topPnl.setPreferredSize(new Dimension(WIDTH, TOP_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));	//set this panel to 2/16 of the JFrame
		//topPnl.setLayout(new GridLayout(3,1,5,5)); //3 rows, 1 columns, hgap = 5, vgap = 5
		JLabel nameLbl 		= new JLabel("Create Conference", JLabel.CENTER);      //create labels for user info 
		nameLbl.setPreferredSize(new Dimension(WIDTH, 20));
		JLabel hqLbl 		= new JLabel("Logged in as: " + hq.getName(), JLabel.LEFT);      //create labels for user
		hqLbl.setPreferredSize(new Dimension(WIDTH - 50, 20));
		JLabel roleLbl		= new JLabel("Role:                " + Roles.HQ.toString(), JLabel.LEFT);
		roleLbl.setPreferredSize(new Dimension(WIDTH - 50, 20));
		topPnl.add(nameLbl);
		topPnl.add(hqLbl);
		topPnl.add(roleLbl);
		
		add(topPnl);	
		return topPnl;
	}
	

	private JPanel createMidPanel() {
		JPanel midPnl = new JPanel();
		midPnl.setBackground(Color.WHITE);
		midPnl.setPreferredSize(new Dimension(WIDTH, MID_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		midPnl.setLayout(layout);
		
		JLabel confLbl = new JLabel("Conference: ", JLabel.RIGHT);
		confLbl.setPreferredSize(new Dimension(260, 20));
		confText = new JTextField("Enter a conference name", 30);
	
		JLabel dateLbl = new JLabel("Date: ", JLabel.RIGHT);
		dateLbl.setPreferredSize(new Dimension(260, 20));
		dateText = new JTextField("When: mm/dd/yyyy", 30);
		
		JLabel venueLbl = new JLabel("Venue:", JLabel.RIGHT);
		venueLbl.setPreferredSize(new Dimension(260, 20));
		venueText = new JTextField("Where", 30);

		JLabel deadlineLbl = new JLabel("Deadline:", JLabel.RIGHT);
		deadlineLbl.setPreferredSize(new Dimension(260, 20));
		deadlineText = new JTextField("Due: mm/dd/yyyy", 30);
		
		JLabel pcLbl = new JLabel ("Program Chair:", JLabel.RIGHT);
		pcLbl.setPreferredSize(new Dimension(260, 20));
		
		pcBox = new JComboBox(hq.getUserList(hq.getName()).toArray());
		pcBox.setPreferredSize(new Dimension(335, 20));
			
		midPnl.add(confLbl);
		midPnl.add(confText);
		midPnl.add(dateLbl);
		midPnl.add(dateText);
		midPnl.add(venueLbl);
		midPnl.add(venueText);
		midPnl.add(deadlineLbl);
		midPnl.add(deadlineText);
		midPnl.add(pcLbl);
		midPnl.add(pcBox);
	
		add(midPnl);
		return midPnl;
	}
	
	/**
	 * @return
	 */

	private JPanel createBotPanel() {
		JPanel botRgtPnl = new JPanel();
		botRgtPnl.setBackground(Color.LIGHT_GRAY);
		botRgtPnl.setPreferredSize(new Dimension(WIDTH / 2, BOT_PANEL_HEIGHT * HEIGHT / FRAME_HEIGHT));  //set this panel to 3/16 of the JFrame
		FlowLayout layout = new FlowLayout(FlowLayout.CENTER);
		layout.setVgap(30);										//vertical gap for the buttons
		layout.setHgap(25);										//horizontal gap for the buttons
		botRgtPnl.setLayout(layout);
		
		JButton desgBut = new JButton("Create");
		desgBut.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
		desgBut.addActionListener(new ActionListener(){
	        public void actionPerformed(final ActionEvent the_event){
	        	Conference conf = new Conference(confText.getText(), dateText.getText(), 
	        					venueText.getText(), deadlineText.getText());
	        	String name = pcBox.getSelectedItem().toString();
	        	conf.setPc(name);
	        	hq.createConf(conf, name);
	        	mainFrame.dispose();
	        }
	      });
		JButton signoutBut = new JButton("Sign out");
		signoutBut.setPreferredSize(new Dimension(BUTTON_WIDTH, BUTTON_HEIGHT));
		signoutBut.addActionListener(new ActionListener()
	      {
	        public void actionPerformed(final ActionEvent the_event){
	        	mainFrame.dispose();
	        	FileParser fp = new FileParser();
	        	User user = new User(fp);
	    		UserView userView = new UserView(user, fp);	//need file parser to generate lists of conferences
	    		userView.start();
	        }
	      });
		
		botRgtPnl.add(desgBut);
		botRgtPnl.add(signoutBut);

		add(botRgtPnl);
		return botRgtPnl;
	}
	
	
}

